# Load data
panel_data <- readRDS("Data/panel/panel_data.rds")
change_data <- readRDS("Data/panel/change_scores.rds")

# Calculate alpha of perceived out-party SUP items
panel_data %>%
    select(starts_with("norm_") & ends_with("_perception")) %>%
    alpha()

# Calculate days between waves
change_data$days_between <- difftime(
    change_data$second_wave,
    change_data$first_wave,
    unit = "days"
) %>%
    as.numeric()

# Summarize days between waves
change_data$days_between %>%
    mean() %>%
    round(2)

change_data$days_between %>%
    sd() %>%
    round(2)

# Scale change by time between waves
change_data <- change_data %>%
    mutate(
        monthly_norm_perception_index_change = norm_perception_index_change / days_between * 30
    )

# Calculate average change by direction
change_data %>%
    subset(monthly_norm_perception_index_change < 0) %>%
    summarize(
        mean = mean(monthly_norm_perception_index_change, na.rm = TRUE),
        se = sd(monthly_norm_perception_index_change, na.rm = TRUE) / sqrt(n())
    ) %>%
    round(2)

change_data %>%
    subset(monthly_norm_perception_index_change > 0) %>%
    summarize(
        mean = mean(monthly_norm_perception_index_change, na.rm = TRUE),
        se = sd(monthly_norm_perception_index_change, na.rm = TRUE) / sqrt(n())
    ) %>%
    round(2)

# Compare monthly change to experimental effect
mean(change_data$monthly_norm_perception_index_change <= -10.7, na.rm = TRUE) * 100
mean(change_data$monthly_norm_perception_index_change >= 10.7, na.rm = TRUE) * 100

change_data %>%
    summarize(
        mean = mean(monthly_norm_perception_index_change, na.rm = TRUE),
        se = sd(monthly_norm_perception_index_change, na.rm = TRUE) / sqrt(n())
    ) %>%
    round(2)

# Set visual parameters
source("Code/ggplot2_theme.R")

# Plot
change_data %>%
    ggplot() +
    coord_cartesian(
        xlim = c(-50, 50),
        expand = FALSE
    ) +
    annotate(
        geom = "rect", xmin = -50, xmax = -10.7, ymin = 0, ymax = Inf,
        fill = "#D55E00", alpha = 0.2
    ) +
    annotate(
        geom = "rect", xmin = 10.7, xmax = 50, ymin = 0, ymax = Inf,
        fill = "#0072B2", alpha = 0.2
    ) +
    geom_density(
        aes(monthly_norm_perception_index_change),
        fill = "black"
    ) +
    geom_vline(xintercept = -10.7, color = "#D55E00") +
    geom_vline(xintercept = 0, color = "white", linetype = "dashed") +
    geom_vline(xintercept = 10.7, color = "#0072B2") +
    geom_text(
        aes(x = -12.7, y = 0.05),
        label = "Over-Time Change\nLarger Than\nCorrection Effect\n(15.9%)",
        colour = "#D55E00",
        size = 5,
        hjust = 1
    ) +
    geom_text(
        aes(x = 12.7, y = 0.05),
        label = "Over-Time Change\nLarger Than\nCorrection Effect\n(15.9%)",
        colour = "#0072B2",
        size = 5,
        hjust = 0
    ) +
    theme_prl() +
    theme(
        axis.title.x = element_markdown(),
        axis.title.y = element_markdown()
    ) +
    labs(
        y = "**Proportion of Change Scores**<br>",
        x = "<br>**Estimated Change in Perceived Out-Party**<br>**Support for Undemocratic Practices**<br>(Over 30 Days)"
    )

## Save plot
ggsave(
    "Images/dist_of_monthly_change.png",
    width = 7, height = 5
)
